package com.hxd.otc.module.coin_deal.activity;

import android.text.TextUtils;
import android.view.View;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseRecyclerActivity;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.dialog.CreateEntryOrdersDialog;
import com.hxd.otc.dialog.ForbiddenBankCardDialog;
import com.hxd.otc.module.coin_deal.adapter.MyEntryOrdersAdapter;
import com.hxd.otc.module.coin_deal.bean.CoinDealResultBean;
import com.hxd.otc.module.coin_deal.bean.DialogCoinTypeBean;
import com.hxd.otc.module.coin_deal.contract.MyEntryOrdersContract;
import com.hxd.otc.module.coin_deal.presenter.MyEntryOrdersPresenter;
import com.hxd.otc.widget.TitleBar;
import com.hxd.otc.widget.WalletDialogTipsUtil;

import java.util.List;

import butterknife.BindView;

/**
 * Created by lzy on 2018/7/9 0009.
 * description ${我的挂单}
 */
public class MyEntryOrdersActivity extends BaseRecyclerActivity<MyEntryOrdersPresenter, MyEntryOrdersAdapter, CoinDealResultBean.DataBean> implements MyEntryOrdersContract.View, CreateEntryOrdersDialog.OnCreateEntryOrdersListener, MyEntryOrdersAdapter.OnDeleteEntryOrdersListener {

    @BindView(R.id.title_bar)
    TitleBar mTitleBar;
    private CreateEntryOrdersDialog mCreateEntryOrdersDialog;
    List<DialogCoinTypeBean> coinTypeBeanList;

    @Override
    protected void initData() {
        super.initData();
        mPresenter.getCoinType();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_entry_orders;
    }

    @Override
    protected MyEntryOrdersAdapter createAdapter() {
        return new MyEntryOrdersAdapter(this);
    }

    @Override
    protected MyEntryOrdersPresenter createPresenter() {
        return new MyEntryOrdersPresenter(this);
    }

    @Override
    public boolean isEnableLoadMore() {
        return false;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mAdapter.setOnDeleteEntryOrdersListener(this);
        mTitleBar.setOnTitleBarItemClickListener(new TitleBar.OnTitleBarItemClickListener() {
            @Override
            public void onBack(View v) {
            }

            @Override
            public void onRight(View v) {
                onCreateEntryOrders();
            }
        });
    }

    /**
     * 新增挂单
     */
    private void onCreateEntryOrders() {
        //校验交易密码
        if (!UserHelper.isSetPayPassword()) {
            WalletDialogTipsUtil.showTpwTipsDialog(this);
            return;
        }
        if (coinTypeBeanList == null)
            return;
        mCreateEntryOrdersDialog = new CreateEntryOrdersDialog(MyEntryOrdersActivity.this)
                .setCoinTypes(coinTypeBeanList)
                .setOnCreateEntryOrdersListener(MyEntryOrdersActivity.this);
        mCreateEntryOrdersDialog.show();
    }


    @Override
    public void onCreateEntryOrdersSuccess() {
        mPresenter.onRefresh();
        $setResult();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mCreateEntryOrdersDialog != null) {
            mCreateEntryOrdersDialog.setOnCreateEntryOrdersListener(null);
            mCreateEntryOrdersDialog.dismiss();
            mCreateEntryOrdersDialog.onDestory();
            mCreateEntryOrdersDialog = null;
        }
        if (mDeleteEntryOrdersDialog != null) {
            if (mDeleteEntryOrdersDialog.isShowing()) {
                mDeleteEntryOrdersDialog.dismiss();
            }
            mDeleteEntryOrdersDialog = null;
        }
    }

    private ForbiddenBankCardDialog<CoinDealResultBean.DataBean> mDeleteEntryOrdersDialog;

    @Override
    public void onDeleteEntryOrders(int position) {
        CoinDealResultBean.DataBean item = mAdapter.getItem(position);
        if (mDeleteEntryOrdersDialog == null) {
            mDeleteEntryOrdersDialog = new ForbiddenBankCardDialog<CoinDealResultBean.DataBean>(this)
                    .setMessage("是否确认删除挂单")
                    .setOnConfirmClickListener(new ForbiddenBankCardDialog.OnConfirmClickListener<CoinDealResultBean.DataBean>() {
                        @Override
                        public void onConfirm(CoinDealResultBean.DataBean data) {
                            mPresenter.deleteEntryOrders(data.getId());
                        }
                    });
        }
        mDeleteEntryOrdersDialog.setData(item).show();
    }

    @Override
    public void deleteEntryOrdersSuccess(String id, String msg) {
        showToast(TextUtils.isEmpty(msg) ? getString(R.string.delete_entry_orders_success) : msg);
        for (int i = 0; i < mAdapter.getItems().size(); i++) {
            CoinDealResultBean.DataBean item = mAdapter.getItem(i);
            if (id.equals(item.getId())) {
                mAdapter.remove(i);
                break;
            }
        }
    }

    @Override
    public void getCoinType(List<DialogCoinTypeBean> coinTypeBeans) {
        coinTypeBeanList = coinTypeBeans;
    }

}
